<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\duty\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 信息管理
 * @package app\news
 * @author LJQ<245369209@qq.com>
 */
class News extends Api

{

	protected $wfType = 13;
	
    /**
   *  信息列表
   * @param string 
   * @return Json
   */

	public function newsList($newsId=null){
             $where= [] ;
             if ($newsId) {
             $where['id']=['eq',$newsId]; 
             }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 信息列表
			$info =collection(model('news')->with('user')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//信息列表
			$info =collection(model('news')->with('user')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('news')->where($where)->count('id'); 
		
    		      if ($info && $count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到信息','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'信息','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加信息
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('duty/news');
          $result = $model->change($data);
          if (false != $result) {
            setlog('newsadd','news',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'信息添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'信息添加失败,请检查']);
          }
      }

   	

   /**
   * 修改信息
   * @param  string newsId 信息id,
   * @return Json
   */
   	public function edit($data,$newsId){
        
          if (!$newsId) {
              return json(['code'=>0,'msg'=>'信息不存在,请检查']);
          }
          $model = model('duty/news');     
          if ($model->change($data)) {
            setlog('newsupdate','news',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'信息修改成功']);
          }else{
             return json(['code'=>0,'msg'=>'信息信息修改失败,请检查']);
          }
    }
    /**
   *  删除信息
   * @param string newsId 信息id,
   * @param Json
   */
     
    public function del($newsId) {
        $newsId = input('newsId',0,'intval');
        $model = model('duty/news');
      	if (empty ( $newsId )||empty(($model->where(array('id'=>$newsId))->find()))) {
           	return  json(['code'=>0,'msg'=>'信息id不存在,请检查']);
        }
        if($model->destroy(['id'=>$newsId]) !== FALSE){
          setlog('newsdel','news',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'信息删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'信息删除失败,请检查']);
       }
    }

     
   /**
   *   查询信息
   * @param keyword信息名称
   * @return Json
   */
   	public function search($start=null,$end=null,$keyword=null){
   		$where = [] ;

		if($keyword){
			
			$where['title'] = ['like','%'.$keyword.'%'];
		}

            if($start&&$end){
                 $start=strtotime($start);
                 $end=strtotime($end);

                 $where['createTime'] = ['between time',[$start,$end]];
            }

   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//信息列表
			$info =collection(model('news')->with('user')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//信息列表
			$info =collection(model('news')->with('user')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('news')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到信息','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'信息','count'=>$count,'data'=>$info]);
       	}
      }
      

     	/**
     	 * 申请审核
     	 * @param string $hazardId
     	 */
     	public function audit($newsId = null){
     		$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
     		if(empty($workflow)||empty($workflow->steps)){
     			return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
     		}else{
     			$news = model('News')->get($newsId);
     			if($news->isUpdate(true)->save(['checkStatus'=>2]) !== false){
     				$data['title'] = $news['title'];
     				$data['wfFlowInfoId'] = $workflow['id'];
     				$data['wfType'] = $this->wfType;
     				$data['busiId'] = $newsId;
     				$data['applyPerson'] = session('user_auth.id');
     				if(model('Wfitem')->save($data)){
     					return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
     				}else{
     					return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
     				}
     			}
     		}
     		return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
     		 
     	}
     
}